package com.shujia.scala

import java.sql.{Connection, DriverManager, PreparedStatement, ResultSet, Statement}

object Demo23Jdbc {
  def main(args: Array[String]): Unit = {

    //创建数据连接对象//创建数据连接对象
    val conn: Connection = DriverManager.getConnection("jdbc:mysql://rm-bp1y7dm47j8h060vy4o.mysql.rds.aliyuncs.com:3307/bigdata32","bigdata32","123456")

    //获取hive操作对象//获取hive操作对象
//    val state: Statement = conn.createStatement
    val pSt: PreparedStatement = conn.prepareStatement("select * from bigdata32.student where clazz = ? limit 10")

    val clazz :String = "文科三班"
    // 设置参数
    pSt.setString(1,clazz)

    //执行sql语句//执行sql语句
    val resultSet: ResultSet = pSt.executeQuery()

    //解析查询结果//解析查询结果
    while (resultSet.next) {
      val id: String = resultSet.getString("id")
      val name: String = resultSet.getString("name")
      val age: Int = resultSet.getInt("age")
      val gender: String = resultSet.getString(4)
      val clazz: String = resultSet.getString(5)
      println(s"$id,$name,$age,$gender,$clazz")
    }

    //关闭资源//关闭资源
    pSt.close()
    conn.close()
  }

}
